package cn.neusoft.service.impl;


import cn.neusoft.domain.CargoDescription;
import cn.neusoft.domain.CargoVariety;
import cn.neusoft.mapper.CargoDescriptionMapper;
import cn.neusoft.mapper.CargoVarietyMapper;
import cn.neusoft.service.CargoAndDescriptionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CargoAndDescriptionImp implements CargoAndDescriptionService {

    @Autowired
    private CargoDescriptionMapper cargoDescriptionMapper;
    @Autowired
    private CargoVarietyMapper cargoVarietyMapper;

    @Override
    public int cargoDesRegister(CargoDescription cargoDescription) {
        int ans = 0;

        if(cargoDescription != null){
            if(cargoDescriptionMapper.findByName(cargoDescription.getName()) != null){
                ans = -1;
            }else{
                ans = cargoDescriptionMapper.insert(cargoDescription);
            }
        }

        return ans;
    }
    @ApiOperation("根据集合元素名称来执行操作")
    @Override
    public int varietyRegister(CargoVariety cargoVariety) {
        int ans = 0;

        if(cargoVariety != null){

            if(cargoVarietyMapper.findByName(cargoVariety.getVarietyName()) != null){
                ans = -1;
            }else{
                if(cargoVariety.getParentID() >= 1){
                    if(cargoVarietyMapper.findByID(cargoVariety.getParentID()) != null){
                        ans = cargoVarietyMapper.insert(cargoVariety);
                    }
                    else{
                        ans = -2;
                    }
                }
                else{

                    ans = cargoVarietyMapper.insert(cargoVariety);

                }
            }
        }

        System.out.println("ans" + ans);

        return ans;
    }

    @Override
    public int varietyDelete(String varName) {
        return cargoVarietyMapper.deleteByName(varName);
    }

    @Override
    public List<CargoVariety> getVarietyByLayers(int layer) {
        List<CargoVariety> list = new ArrayList<CargoVariety>();

        List<CargoVariety> roots = cargoVarietyMapper.getRoots();
        Queue<CargoVariety> queue = new LinkedList<CargoVariety>();

        for(CargoVariety c : roots){
            queue.offer(c);
        }
        for(int i = 1; i <= layer; i++){
            int number = queue.size();
            CargoVariety temp = null;
            for(int j = 1; j <= number; j++){
                temp = queue.poll();
                for(CargoVariety c : temp.getChildrenList()){
                    queue.offer(c);
                }
            }
        }
        while(!queue.isEmpty()){
            list.add(queue.poll());
        }



        return list;
    }

    @Override
    public List<CargoDescription> getCargoDesByVarName(String varName) {
        List<CargoDescription> list = new ArrayList<CargoDescription>();


        if(varName != null){
            CargoVariety cargoVariety = cargoVarietyMapper.findByName(varName);
            if(cargoVariety != null){
                for(String name : cargoVariety.getCargoNameList()){
                    CargoDescription cd = cargoDescriptionMapper.findByName(name);
                    if(cd != null){
                        list.add(cd);
                    }

                }
            }
        }




        return list;
    }

    @Override
    public List<String> getCargoDesNameByVarName(String varName) {
        List<String> list = new ArrayList<String>();


        if(varName != null){
            CargoVariety cargoVariety = cargoVarietyMapper.findByName(varName);
            if(cargoVariety != null){
                Queue<CargoVariety> queue = new LinkedList<CargoVariety>();
                queue.offer(cargoVariety);
                while(!queue.isEmpty()){
                    CargoVariety temp = queue.poll();
                    for(String name : temp.getCargoNameList()){
                        list.add(name);
                    }
                    for(CargoVariety c : temp.getChildrenList()){
                        queue.offer(c);
                    }
                }
            }
        }




        return list;
    }

    @Override
    public int cargoDesDelete(String desName) {
        int ans = 0;

        if(desName != null){
            CargoDescription cd = cargoDescriptionMapper.findByName(desName);
            if(cd != null){
                CargoVariety c = findVarHadDesName(desName);
                if(c != null){
                    System.out.println("desName" + desName);
                    c.getCargoNameList().remove(desName);
                    System.out.println(c.getCargoNameList().size());
                    ans = cargoVarietyMapper.update(c);
                }
            }
            if(ans == 1){
                ans = cargoDescriptionMapper.deleteByID(cd.getId());
            }

        }

        return ans;
    }




    @Override
    public int cargosToVariety(String desName, String varName) {
        int ans = 0;

        if(desName != null && varName != null){
            CargoVariety cargoVariety = cargoVarietyMapper.findByName(varName);
            if(cargoVariety != null){
                CargoDescription cargoDescription = cargoDescriptionMapper.findByName(desName);
                if(cargoDescription != null){
                    CargoVariety c = findVarHadDesName(desName);

                    if(c != null){
                        System.out.println("c!=null");
                        if(cargoVariety.getId() != c.getId()){
                            c.getCargoNameList().remove(desName);
                            ans = cargoVarietyMapper.update(c);
                            if(ans > 0){
                                if(!cargoVariety.getCargoNameList().contains(desName)){
                                    cargoVariety.getCargoNameList().add(desName);
                                    ans = cargoVarietyMapper.update(cargoVariety);
                                }
                            }
                        }

                    }

                }

            }

        }



        return ans;
    }

    public CargoVariety findVarHadDesName(String desName){
        CargoVariety cargoVariety = null;
        if (desName != null){
            List<CargoVariety> roots = cargoVarietyMapper.getRoots();
            Queue<CargoVariety> queue = new LinkedList<CargoVariety>();
            for(CargoVariety c : roots){
                queue.offer(c);
            }
            while(!queue.isEmpty()){
                CargoVariety temp = queue.poll();
                List<String> desNames = temp.getCargoNameList();
                boolean isHad = false;
                for(String name : desNames){
                    if(name.equals(desName)){
                        cargoVariety = temp;
                        System.out.println("null?" + (temp == null));
                        isHad = true;
                        break;
                    }
                }
                if(isHad){
                    break;
                }
                for(CargoVariety c : temp.getChildrenList()){
                    queue.offer(c);
                }
            }
        }
        return cargoVariety;
    }

    @Override
    public CargoDescription getByDesName(String name) {

        return cargoDescriptionMapper.findByName(name);
    }

  @Override
  public int updateCargoDes(CargoDescription cargoDescription) {
      int ans = 0;
    if(cargoDescription != null){
      CargoDescription old = cargoDescriptionMapper.findByID(cargoDescription.getId());
      if(old != null){
        ans = cargoDescriptionMapper.update(cargoDescription);
        if(ans == 1){
          if(!old.getName().equals(cargoDescription.getName())){
            CargoVariety cargoVariety = findVarHadDesName(old.getName());
            if(cargoVariety != null){
              cargoVariety.getCargoNameList().remove(cargoDescription.getName());
              ans = cargoVarietyMapper.update(cargoVariety);
            }
          }
        }
      }

    }

    return ans;
  }

  @Override
  public int updateVariety(CargoVariety cargoVariety) {
      int ans = 0;
      if(cargoVariety != null){
          if(cargoVariety.getParentID() == cargoVariety.getId()){
              return -3;
          }
          if(cargoVariety.getParentID() >= 1){
              CargoVariety parent = cargoVarietyMapper.findByID(cargoVariety.getParentID());
              if(parent == null){
                  ans = -1;
                  return ans;
              }

          }
          ans = cargoVarietyMapper.update(cargoVariety);


      }
      return ans;
  }

  @Override
  public CargoVariety getVarietyByName(String varName) {
    if(varName != null){
      return cargoVarietyMapper.findByName(varName);
    }
    else{
      return null;
    }
  }

  @Override
  public List<CargoVariety> getVarietyRoot() {
    return cargoVarietyMapper.getRoots();
  }

    @Override
    public CargoVariety getVarietyByID(int id) {
        return cargoVarietyMapper.findByID(id);
    }

    @Override
    public CargoDescription getDesByID(int id) {
        return cargoDescriptionMapper.findByID(id);
    }

    @Override
    public List<CargoVariety> GetParentVar(String varName) {
        CargoVariety cargoVariety = cargoVarietyMapper.findByName(varName);
        List<CargoVariety> list = new ArrayList<>();
        if(varName != null){
            CargoVariety temp = cargoVariety;
            Stack<CargoVariety> stack = new Stack<>();
            stack.push(cargoVariety);
            while(true){
                if(temp.getParentID() >= 1){
                    CargoVariety parent = cargoVarietyMapper.findByID(temp.getParentID());
                    if(parent != null){
                        temp = parent;
                        stack.push(temp);
                        continue;
                    }
                }
                break;
            }
            while(!stack.isEmpty()){
                list.add(stack.pop());
            }
        }

        return list;
    }

    @Override
    public List<CargoDescription> getAllDes() {
        return cargoDescriptionMapper.getAll();
    }


}
